import { ElMessage } from 'element-plus'

// str 颜色值字符串
export const hexToRgb = (str: string): any => {
    let hexs: any = ''
    let reg = /^\#?[0-9A-Fa-f]{6}$/
    if (!reg.test(str)) {
        ElMessage.warning('输入错误的hex')
        return ''
    }
    str = str.replace('#', '')
    hexs = str.match(/../g)
    for (let i = 0; i < 3; i++) hexs[i] = parseInt(hexs[i], 16)
    return hexs
}
// r 代表红色 | g 代表绿色 | b 代表蓝色
export const rgbToHex = (r: number, g: number, b: number): string => {
    let reg = /^\d{1,3}$/
    if (!reg.test(r.toString()) || !reg.test(g.toString()) || !reg.test(b.toString())) {
        ElMessage.warning('输入错误的rgb颜色值')
        return ''
    }
    let hexs = [r.toString(16), g.toString(16), b.toString(16)]
    for (let i = 0; i < 3; i++) if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}`
    return `#${hexs.join('')}`
}
// color 颜色值字符串 | level 变浅的程度，限0-1之间
export const getDarkColor = (color: string, level: number): string => {
    let reg = /^\#?[0-9A-Fa-f]{6}$/
    if (!reg.test(color)) {
        ElMessage.warning('输入错误的hex颜色值')
        return ''
    }
    let rgb = hexToRgb(color)
    for (let i = 0; i < 3; i++) rgb[i] = Math.floor(rgb[i] * (1 - level))
    return rgbToHex(rgb[0], rgb[1], rgb[2])
}
// color 颜色值字符串 | level 加深的程度，限0-1之间
export const getLightColor = (color: string, level: number): string => {
    let reg = /^\#?[0-9A-Fa-f]{6}$/
    if (!reg.test(color)) {
        ElMessage.warning('输入错误的hex颜色值')
        return ''
    }
    let rgb = hexToRgb(color)
    for (let i = 0; i < 3; i++) rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i])
    return rgbToHex(rgb[0], rgb[1], rgb[2])
}
// 获得随机颜色
export const getRandomColor = () => {
    const r = Math.floor(Math.random() * 256)
    const g = Math.floor(Math.random() * 256)
    const b = Math.floor(Math.random() * 256)
    return rgbToHex(r, g, b)
}
